import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token'
// import Login from '@/views/Login/'
// import Layout from '@/views/Layout'
// import User from '@/views/User'
// import Home from '@/views/Home'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEdit from '@/views/User/UserEdit.vue'
// import Chat from '@/views/Chat'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "Login" */ '@/views/Login/'),
    // 路由独享守卫
    beforeEnter (to, from, next) {
      if (getToken()) { // 本地有token 并且去往的是登录页 注意：to.path 是带'/'的
        // 拦截留在当前页面
        // next(false)
        // 强制跳转到首页
        next('/layout/home')
      } else {
        // 其他路由跳转正常放行
        next()
      }
    }
  },
  {
    path: '/layout',
    component: () => import(/* webpackChunkName: "Layout" */ '@/views/Layout'),
    // 二级路由
    children: [
      {
        path: 'user',
        component: () => import(/* webpackChunkName: "User" */ '@/views/User')
      },
      {
        path: 'home',
        component: () => import(/* webpackChunkName: "Home" */ '@/views/Home'),
        meta: {
          scrollTop: 0 // 记录滚动条的位置
        }
      }
    ]
  },
  {
    path: '/search',
    component: () => import(/* webpackChunkName: "Search" */ '@/views/Search')
  },
  // 文章结果
  {
    path: '/search_result:kw',
    component: () => import(/* webpackChunkName: "SearchResult" */ '@/views/Search/SearchResult.vue')
  },
  // 文章详情
  {
    path: '/detail',
    component: () => import(/* webpackChunkName: "Detail" */ '@/views/ArticleDetail')
  },
  // 编辑用户信息
  {
    path: '/user_edit',
    component: () => import(/* webpackChunkName: "UserEdit" */ '@/views/User/UserEdit.vue')
  },
  // 小思同学
  {
    path: '/chat',
    component: () => import(/* webpackChunkName: "Chat" */ '@/views/Chat')
  }

]

const router = new VueRouter({
  routes
})
// 全局前置守卫(在路由真正跳转之前，执行此函数)
// router.beforeEach((to, from, next) => {
//   if (getToken() && to.path === '/login') { // 本地有token 并且去往的是登录页 注意：to.path 是带'/'的
//     // 拦截留在当前页面
//     next(false)
// 强制跳转到首页
//  next('/layout/home')
//   } else {
//     // 其他路由跳转正常放行
//     next()
//   }
// })
export default router
